/*
 * @(#)PIXCollector
 *
 * Copyright 2001-2005 友联创新科技
 *
 */
package com.sinodata.bsm.cicp.collector.net.cisco;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;
import com.union.net.snmp.SnmpGet;

/**
 * PIXCollector.java
 * <p>
 * PIX防火墙指标采集
 *
 * @author 李祥军 lixj@uni-mon.com
 * @version 1.0
 * @Date 2010-6-29
 */
public class PIXCollector extends Collector {
    static Logger logger = Logger.getLogger(PIXCollector.class);
    static String[] ParamMeta = { "共同体" };
    static String[] PropMeta = { "CPU使用率", "内存使用率", "当前连接数", "最高连接数", "内存总量", "已用内存", "空闲内存" };

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        SnmpGet snmpGet = null;
        ResPropertyValue propertyValueX = new ResPropertyValue();
        PropValue[] values = new PropValue[7];
        try {
            snmpGet = new SnmpGet(ip);
            snmpGet.setCommunity(getParamMap().get(ParamMeta[0]));
            List<TableEvent> cupValues = snmpGet.getTable(".1.3.6.1.4.1.9.9.109.1.1.1.1.5");
            int count = 0;
            int totalcpu = 0;
            for (TableEvent tableEvent : cupValues) {
                VariableBinding[] vbs = tableEvent.getColumns();
                if (vbs != null & vbs.length > 0) {
                    count += 1;
                    totalcpu += vbs[0].getVariable().toInt();
                }
            }
            values[0] = ResPropertyValue.createPropValue(totalcpu / count + "", PropMeta[0]);
            List<TableEvent> usedMems = snmpGet.getTable("1.3.6.1.4.1.9.9.48.1.1.1.5");
            int usedMem = 0;
            for (TableEvent tableEvent : usedMems) {
                VariableBinding[] vbs = tableEvent.getColumns();
                if (vbs != null & vbs.length > 0) {
                    usedMem += vbs[0].getVariable().toInt() / (1024 * 1024);
                }
            }
            values[5] = ResPropertyValue.createPropValue(usedMem + "", PropMeta[5]);
            List<TableEvent> freeMems = snmpGet.getTable("1.3.6.1.4.1.9.9.48.1.1.1.6");
            int freemem = 0;
            for (TableEvent tableEvent : freeMems) {
                VariableBinding[] vbs = tableEvent.getColumns();
                if (vbs != null & vbs.length > 0) {
                    freemem += vbs[0].getVariable().toInt() / (1024 * 1024);
                }
            }
            values[6] = ResPropertyValue.createPropValue(freemem + "", PropMeta[6]);
            values[4] = ResPropertyValue.createPropValue(usedMem + freemem + "", PropMeta[4]);
            values[1] = ResPropertyValue.createPropValue((usedMem * 100 / (usedMem + freemem)) + "", PropMeta[1]);
            //实际情况是pix防火墙的
            //当前连接数的oid为 .1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6
            //最大连接数的oid为 .1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.7
            //由于snmpGet.getSingle的方法中用的pdu的getnext类型，所以用以下的oid
            String currentConn = snmpGet.getSingle("1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.5");
            String maxConn = snmpGet.getSingle("1.3.6.1.4.1.9.9.147.1.2.2.2.1.5.40.6");
            values[2] = ResPropertyValue.createPropValue(currentConn, PropMeta[2]);
            values[3] = ResPropertyValue.createPropValue(maxConn, PropMeta[3]);

            propertyValueX.setValues(values);
            return Arrays.asList(propertyValueX);
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        } finally {
            if (snmpGet != null)
                snmpGet.close();
        }
    }

    public String[] getParamMeta() {
        return ParamMeta;
    }

    public String[] getPropMeta() {
        return PropMeta;
    }

}
